
package example.main;

import fi.serv.btk.bencode.BEncode;
import fi.serv.btk.bencode.BEncode.BItem;
import fi.serv.btk.bencode.BEncode.EncodedItem;
import fi.serv.btk.bencode.BEncode.InvalidObject;
import fi.serv.btk.bencode.BEncode.ParsingFailed;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * @author btk
 */
public class Example
{

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args)
  {
    try
    {
      Map<String, Object> root = new HashMap<>();
      root.put("foo", "foo value");
      root.put("one", 1);
      
      List<Object> list = new ArrayList<>();
      list.add(1);
      list.add("bar");
      
      root.put("list", list);
      
      Map<String, Object> dict = new HashMap<>();
      dict.put("some", "value");
      dict.put("more", "data");
      
      // we can create encode only parts of data
      EncodedItem intermidiate = BEncode.encode(dict);
      
      root.put("dict", intermidiate);
      
      ByteArrayOutputStream oStream = new ByteArrayOutputStream();
      BEncode.encode(oStream, root);
      
      ByteArrayInputStream iStream = new ByteArrayInputStream(oStream.toByteArray());
      
      BItem rootNode = BEncode.decode(iStream);
      
      System.out.println(rootNode.get("foo").getString());
      System.out.println(rootNode.get("dict").get("some").getString());
      System.out.println(rootNode.get("list").get(0).getLong());
    }
    catch(ParsingFailed ex)
    {
    }
    catch(IOException ex)
    {
    }
    catch(InvalidObject ex)
    {
    }
  }
}
